package o;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public final class dq<E> implements Collection<E>, Set<E> {
    @Nullable
    private static Object[] ʽ;
    private static final int[] ˊ = new int[0];
    @Nullable
    private static Object[] ˎ;
    private static final Object[] ॱ = new Object[0];
    private static int ॱॱ;
    private static int ᐝ;
    private dv<E, E> ʻ;
    private int[] ʼ;
    int ˋ;
    Object[] ˏ;

    private int ˊ(Object obj, int i) {
        int i2 = this.ˋ;
        if (i2 == 0) {
            return -1;
        }
        int ˋ = dt.ˋ(this.ʼ, i2, i);
        if (ˋ < 0 || obj.equals(this.ˏ[ˋ])) {
            return ˋ;
        }
        int i3 = ˋ + 1;
        while (i3 < i2 && this.ʼ[i3] == i) {
            if (obj.equals(this.ˏ[i3])) {
                return i3;
            }
            i3++;
        }
        ˋ--;
        while (ˋ >= 0 && this.ʼ[ˋ] == i) {
            if (obj.equals(this.ˏ[ˋ])) {
                return ˋ;
            }
            ˋ--;
        }
        return i3 ^ -1;
    }

    private int ˎ() {
        int i = this.ˋ;
        if (i == 0) {
            return -1;
        }
        int ˋ = dt.ˋ(this.ʼ, i, 0);
        if (ˋ < 0 || this.ˏ[ˋ] == null) {
            return ˋ;
        }
        int i2 = ˋ + 1;
        while (i2 < i && this.ʼ[i2] == 0) {
            if (this.ˏ[i2] == null) {
                return i2;
            }
            i2++;
        }
        ˋ--;
        while (ˋ >= 0 && this.ʼ[ˋ] == 0) {
            if (this.ˏ[ˋ] == null) {
                return ˋ;
            }
            ˋ--;
        }
        return i2 ^ -1;
    }

    private void ˏ(int i) {
        Object[] objArr;
        if (i == 8) {
            synchronized (dq.class) {
                if (ʽ != null) {
                    objArr = ʽ;
                    this.ˏ = objArr;
                    ʽ = (Object[]) objArr[0];
                    this.ʼ = (int[]) objArr[1];
                    objArr[1] = null;
                    objArr[0] = null;
                    ᐝ--;
                    return;
                }
            }
        } else if (i == 4) {
            synchronized (dq.class) {
                if (ˎ != null) {
                    objArr = ˎ;
                    this.ˏ = objArr;
                    ˎ = (Object[]) objArr[0];
                    this.ʼ = (int[]) objArr[1];
                    objArr[1] = null;
                    objArr[0] = null;
                    ॱॱ--;
                    return;
                }
            }
        }
        this.ʼ = new int[i];
        this.ˏ = new Object[i];
    }

    private static void ॱ(int[] iArr, Object[] objArr, int i) {
        int i2;
        if (iArr.length == 8) {
            synchronized (dq.class) {
                if (ᐝ < 10) {
                    objArr[0] = ʽ;
                    objArr[1] = iArr;
                    for (i2 = i - 1; i2 >= 2; i2--) {
                        objArr[i2] = null;
                    }
                    ʽ = objArr;
                    ᐝ++;
                }
            }
        } else if (iArr.length == 4) {
            synchronized (dq.class) {
                if (ॱॱ < 10) {
                    objArr[0] = ˎ;
                    objArr[1] = iArr;
                    for (i2 = i - 1; i2 >= 2; i2--) {
                        objArr[i2] = null;
                    }
                    ˎ = objArr;
                    ॱॱ++;
                }
            }
        }
    }

    public dq() {
        this(0);
    }

    public dq(int i) {
        if (i == 0) {
            this.ʼ = ˊ;
            this.ˏ = ॱ;
        } else {
            ˏ(i);
        }
        this.ˋ = 0;
    }

    public void clear() {
        if (this.ˋ != 0) {
            ॱ(this.ʼ, this.ˏ, this.ˋ);
            this.ʼ = ˊ;
            this.ˏ = ॱ;
            this.ˋ = 0;
        }
    }

    public void ˎ(int i) {
        if (this.ʼ.length < i) {
            Object obj = this.ʼ;
            Object obj2 = this.ˏ;
            ˏ(i);
            if (this.ˋ > 0) {
                System.arraycopy(obj, 0, this.ʼ, 0, this.ˋ);
                System.arraycopy(obj2, 0, this.ˏ, 0, this.ˋ);
            }
            ॱ(obj, obj2, this.ˋ);
        }
    }

    public boolean contains(@Nullable Object obj) {
        return ˋ(obj) >= 0;
    }

    public int ˋ(@Nullable Object obj) {
        return obj == null ? ˎ() : ˊ(obj, obj.hashCode());
    }

    @Nullable
    public E ॱ(int i) {
        return this.ˏ[i];
    }

    public boolean isEmpty() {
        return this.ˋ <= 0;
    }

    public boolean add(@Nullable E e) {
        int ˎ;
        int i;
        int i2 = 8;
        if (e == null) {
            ˎ = ˎ();
            i = 0;
        } else {
            i = e.hashCode();
            ˎ = ˊ(e, i);
        }
        if (ˎ >= 0) {
            return false;
        }
        ˎ ^= -1;
        if (this.ˋ >= this.ʼ.length) {
            if (this.ˋ >= 8) {
                i2 = this.ˋ + (this.ˋ >> 1);
            } else if (this.ˋ < 4) {
                i2 = 4;
            }
            Object obj = this.ʼ;
            Object obj2 = this.ˏ;
            ˏ(i2);
            if (this.ʼ.length > 0) {
                System.arraycopy(obj, 0, this.ʼ, 0, obj.length);
                System.arraycopy(obj2, 0, this.ˏ, 0, obj2.length);
            }
            ॱ(obj, obj2, this.ˋ);
        }
        if (ˎ < this.ˋ) {
            System.arraycopy(this.ʼ, ˎ, this.ʼ, ˎ + 1, this.ˋ - ˎ);
            System.arraycopy(this.ˏ, ˎ, this.ˏ, ˎ + 1, this.ˋ - ˎ);
        }
        this.ʼ[ˎ] = i;
        this.ˏ[ˎ] = e;
        this.ˋ++;
        return true;
    }

    public boolean remove(@Nullable Object obj) {
        int ˋ = ˋ(obj);
        if (ˋ < 0) {
            return false;
        }
        ˊ(ˋ);
        return true;
    }

    public E ˊ(int i) {
        int i2 = 8;
        E e = this.ˏ[i];
        if (this.ˋ <= 1) {
            ॱ(this.ʼ, this.ˏ, this.ˋ);
            this.ʼ = ˊ;
            this.ˏ = ॱ;
            this.ˋ = 0;
        } else if (this.ʼ.length <= 8 || this.ˋ >= this.ʼ.length / 3) {
            this.ˋ--;
            if (i < this.ˋ) {
                System.arraycopy(this.ʼ, i + 1, this.ʼ, i, this.ˋ - i);
                System.arraycopy(this.ˏ, i + 1, this.ˏ, i, this.ˋ - i);
            }
            this.ˏ[this.ˋ] = null;
        } else {
            if (this.ˋ > 8) {
                i2 = this.ˋ + (this.ˋ >> 1);
            }
            Object obj = this.ʼ;
            Object obj2 = this.ˏ;
            ˏ(i2);
            this.ˋ--;
            if (i > 0) {
                System.arraycopy(obj, 0, this.ʼ, 0, i);
                System.arraycopy(obj2, 0, this.ˏ, 0, i);
            }
            if (i < this.ˋ) {
                System.arraycopy(obj, i + 1, this.ʼ, i, this.ˋ - i);
                System.arraycopy(obj2, i + 1, this.ˏ, i, this.ˋ - i);
            }
        }
        return e;
    }

    public int size() {
        return this.ˋ;
    }

    @NonNull
    public Object[] toArray() {
        Object obj = new Object[this.ˋ];
        System.arraycopy(this.ˏ, 0, obj, 0, this.ˋ);
        return obj;
    }

    @NonNull
    public <T> T[] toArray(@NonNull T[] tArr) {
        Object obj;
        if (tArr.length < this.ˋ) {
            obj = (Object[]) Array.newInstance(tArr.getClass().getComponentType(), this.ˋ);
        } else {
            obj = tArr;
        }
        System.arraycopy(this.ˏ, 0, obj, 0, this.ˋ);
        if (obj.length > this.ˋ) {
            obj[this.ˋ] = null;
        }
        return obj;
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof Set)) {
            return false;
        }
        Set set = (Set) obj;
        if (size() != set.size()) {
            return false;
        }
        int i = 0;
        while (i < this.ˋ) {
            try {
                if (!set.contains(ॱ(i))) {
                    return false;
                }
                i++;
            } catch (NullPointerException e) {
                return false;
            } catch (ClassCastException e2) {
                return false;
            }
        }
        return true;
    }

    public int hashCode() {
        int i = 0;
        int[] iArr = this.ʼ;
        int i2 = 0;
        while (i < this.ˋ) {
            i2 += iArr[i];
            i++;
        }
        return i2;
    }

    public String toString() {
        if (isEmpty()) {
            return "{}";
        }
        StringBuilder stringBuilder = new StringBuilder(this.ˋ * 14);
        stringBuilder.append('{');
        for (int i = 0; i < this.ˋ; i++) {
            if (i > 0) {
                stringBuilder.append(", ");
            }
            dq ॱ = ॱ(i);
            if (ॱ != this) {
                stringBuilder.append(ॱ);
            } else {
                stringBuilder.append("(this Set)");
            }
        }
        stringBuilder.append('}');
        return stringBuilder.toString();
    }

    private dv<E, E> ˏ() {
        if (this.ʻ == null) {
            this.ʻ = new dv<E, E>(this) {
                final /* synthetic */ dq ˋ;

                {
                    this.ˋ = r1;
                }

                protected int ˋ() {
                    return this.ˋ.ˋ;
                }

                protected Object ॱ(int i, int i2) {
                    return this.ˋ.ˏ[i];
                }

                protected int ॱ(Object obj) {
                    return this.ˋ.ˋ(obj);
                }

                protected int ˏ(Object obj) {
                    return this.ˋ.ˋ(obj);
                }

                protected Map<E, E> ˊ() {
                    throw new UnsupportedOperationException("not a map");
                }

                protected void ˊ(E e, E e2) {
                    this.ˋ.add(e);
                }

                protected E ˋ(int i, E e) {
                    throw new UnsupportedOperationException("not a map");
                }

                protected void ˎ(int i) {
                    this.ˋ.ˊ(i);
                }

                protected void ॱ() {
                    this.ˋ.clear();
                }
            };
        }
        return this.ʻ;
    }

    public Iterator<E> iterator() {
        return ˏ().ˏ().iterator();
    }

    public boolean containsAll(@NonNull Collection<?> collection) {
        for (Object contains : collection) {
            if (!contains(contains)) {
                return false;
            }
        }
        return true;
    }

    public boolean addAll(@NonNull Collection<? extends E> collection) {
        ˎ(this.ˋ + collection.size());
        boolean z = false;
        for (Object add : collection) {
            z |= add(add);
        }
        return z;
    }

    public boolean removeAll(@NonNull Collection<?> collection) {
        boolean z = false;
        for (Object remove : collection) {
            z |= remove(remove);
        }
        return z;
    }

    public boolean retainAll(@NonNull Collection<?> collection) {
        boolean z = false;
        for (int i = this.ˋ - 1; i >= 0; i--) {
            if (!collection.contains(this.ˏ[i])) {
                ˊ(i);
                z = true;
            }
        }
        return z;
    }
}
